/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.app.brilliant.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author geekidea
 * @date 2018-11-08
 */
public class DateUtil {

    public static final String formatStr_yyyyMMddHHmmssS_ = "yyyyMMddHHmmss";
    public static final String formatStr_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss.S";
    public static final String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static final String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";
    public static final String formatStr_yyyyMMdd = "yyyy-MM-dd";
    public static final String formatStr_yyyy = "yyyy";
    public static final String formatStr_yyyy_MM_dd = "yyyyMMdd";
    public static final String formatStr_yyyyMMddDelimiter = "-";

    public static String getYYYYMMDDHHMMSS(Date date){
        if (date == null){
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = simpleDateFormat.format(date);
        return dateString;
    }

    public static Date getNextDate(int our,int CalendarField){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        calendar.add(CalendarField,our);
        long minite = calendar.getTimeInMillis();
        return new Date(minite);
    }

    public static Date getNextDate(Date date,int our,int CalendarField){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(CalendarField,our);
        long minite = calendar.getTimeInMillis();
        return new Date(minite);
    }

    public static long getNextDayZeroTimeSpaceSecound() {
        Calendar current = Calendar.getInstance();
        current.setTime(new Date(System.currentTimeMillis()));
        Calendar tomoryZero = Calendar.getInstance();
        tomoryZero.set(Calendar.YEAR,current.get(Calendar.YEAR));
        tomoryZero.set(Calendar.MONTH,current.get(Calendar.MONTH));
        tomoryZero.set(Calendar.DATE,current.get(Calendar.DATE));
        tomoryZero.set(Calendar.HOUR_OF_DAY, 0);
        tomoryZero.set(Calendar.MINUTE, 0);
        tomoryZero.set(Calendar.SECOND, 0);
        tomoryZero.set(Calendar.MILLISECOND,0);
        tomoryZero.add(Calendar.DATE, 1);
        return (tomoryZero.getTimeInMillis()-current.getTimeInMillis())/1000;
    }

    public static Date getThisWeekMonday() {
        Date date = new Date(System.currentTimeMillis());
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    public static Date getEndTime(String type) {
        Date next = null;
        if(type.equals("month")){
            next = DateUtil.getNextDate(1, Calendar.MONTH);
        }else if(type.equals("year")){
            next = DateUtil.getNextDate(1, Calendar.YEAR);
        }else if(type.equals("forever")){
            next = DateUtil.getNextDate(50, Calendar.YEAR);
        }
        return next;
    }
}
